﻿using System;
using System.Collections.Generic;
using WAD.WEB.AppCode;
using System.Text;
using WAD.Model;
using WAD.DAL;

namespace WAD.WEB.Admin.Ajax
{
    /// <summary>
    /// FC_Record 的摘要说明
    /// </summary>
    public class FC_Record : AdminAjax
    {
        public override void AjaxMethodDispatch(String identity)
        {
            switch (identity)
            {
                case "getlist": this.GetList(); break;
                case "getlist_shared": this.GetList_Shared(); break;
                case "gettotal": this.GetTotal(); break;
            }
        }
        /// <summary>
        /// 广告主：交易记录
        /// </summary>
        [QD.Web.Ajax.AjaxMethod]
        public void GetList()
        {
            int pageIndex = Parse.Int("page_index", 1);//当前页
            int userid = Parse.Int("userid", 0);
            int direction = Parse.Int("direction", 0);
            string orderid = Parse.String("orderid", "");

            StringBuilder sb = new StringBuilder();
            sb.Append("1=1");

            if (userid > 0)
                sb.AppendFormat(" and user_id={0}", userid);

            if (direction > 0)
                sb.AppendFormat(" and direction={0}", direction);


            PageData pg = DAOFCRecord.GetListPage("*,(select top 1 title from fc_article where fc_article.id=fc_record.articleid and articleid>0) as title", "id desc", pageIndex, 20, sb.ToString(), "fc_record");
            if (pg.itemCount == 0)
                DataEmpty();
            Success(pg);
        }

        /// <summary>
        /// 分享主的交易记录
        /// </summary>
        [QD.Web.Ajax.AjaxMethod]
        public void GetList_Shared()
        {
            int pageIndex = Parse.Int("page_index", 1);//当前页
            int userid = Parse.Int("userid", 0);
            int direction = Parse.Int("direction", 0);
            string orderid = Parse.String("orderid", "");

            StringBuilder sb = new StringBuilder();
            sb.Append("1=1");

            if (userid > 0)
                sb.AppendFormat(" and user_id={0}", userid);

            if (direction > 0)
                sb.AppendFormat(" and direction={0}", direction);

            PageData pg = DAOFCRecord.GetListPage("*,(select top 1 title from fc_article where fc_article.id=article_id) as title", "date desc", pageIndex, 20, sb.ToString(), "v_fc_shared#cash##union");
            if (pg.itemCount == 0)
                DataEmpty();
            Success(pg);
        }

        /// <summary>
        /// 获取用户的收支统计
        /// </summary>
        [QD.Web.Ajax.AjaxMethod]
        public void GetTotal()
        {
            int userid = Parse.Int("userid", 0);
            int isad = Parse.Int("isad", 0);    //是否是广告主 0不是，1 是

            Dictionary<string, object> dic = new Dictionary<string, object>();
            if (userid < 1)
                Success(dic);

            //总收入
            object direction1 = DAO.ExecuteScalar(string.Format("select sum(price) from fc_shared where by_user_id={0} and state=1 and price>0 and money_id>0 and picurl!=''", userid));

            //总余额
            object balance = DAO.ExecuteScalar(string.Format("select shared_balance from [user] where id={0}", userid));

            PageData pg = DAO.GetList(3, "record_id<1 and user_id=" + userid, "fc_cash", "state,sum(amount)as amount", "", "state");
            for (int i = 0; i < pg.table.Rows.Count; i++)
            {
                if (pg.table.Rows[i]["state"].ToString() == "0")
                    dic.Add("state0", pg.table.Rows[i]["amount"]);

                if (pg.table.Rows[i]["state"].ToString() == "1")
                    dic.Add("state1", pg.table.Rows[i]["amount"]);

                if (pg.table.Rows[i]["state"].ToString() == "2")
                    dic.Add("state2", pg.table.Rows[i]["amount"]);
            }

            dic.Add("balance", DaoUser.BalanceDncode(balance, WJXConfig.EncryptConfig.DES_KEY));
            dic.Add("direction1", direction1);
            Success(dic);
        }

    }
}